<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ page contentType="text/html;charset=utf-8" language="java" %>

<link rel="stylesheet" type="text/css" href="${headerStyleUrl}"/>
<link rel="stylesheet" type="text/css" href="${commonsStyleUrl}"/>

<c:set var="MSG_NONE">
    <spring:message code="MSG_NONE"/>
</c:set>

<%-- Defining username --%>
<c:choose>
    <c:when test="${pageContext.request.userPrincipal.name == null}">
        <c:set var="headUsername">
            <spring:message code="MSG_NONE"/>
        </c:set>
    </c:when>
    <c:otherwise>
        <c:set var="headUsername">
            ${pageContext.request.userPrincipal.name}
        </c:set>
    </c:otherwise>
</c:choose>

<%-- Defining user role --%>
<c:set var="displayManagerContext" value="${false}"/>
<sec:authorize access="hasRole('Manager')">
    <c:set var="userRole"><spring:message code="role.manager"/></c:set>
    <c:set var="displayManagerContext" value="${true}"/>
</sec:authorize>
<sec:authorize access="hasRole('Support')">
    <c:set var="userRole"><spring:message code="role.support"/></c:set>
</sec:authorize>
<c:if test="${userRole == null}">
    <c:set var="userRole"><spring:message code="MSG_NONE"/></c:set>
</c:if>

<script>
    $(document).ready(function () {
        $("#localeSelect").html(
                "<option value=\"ru\" " + '${lang == 'ru'?'selected':''}' + ">Русский</option>" +
                        "<option value=\"en\" " + '${lang == 'en'?'selected':''}' + ">English</option>"
        );
    });
</script>

<%-- Logout stuff --%>
<form action="${logoutUrl}" method="post" id="logoutForm">
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
<script>
    function formSubmit() {
        document.getElementById("logoutForm").submit();
    }
</script>

<table id="headTable">
    <tr>
        <td id="headLocale">
            <span style="text-decoration: underline;"><spring:message code="header.language"/></span>
            <select id="localeSelect" onchange="changeLocale()"></select>
        </td>
        <td id="headUsername">
            <span style="text-decoration: underline;"><spring:message code="header.username"/></span>
            ${headUsername}
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </td>
        <td id="headRole">
            <span style="text-decoration: underline;"><spring:message code="header.role"/></span>
            ${userRole}
        </td>
        <td id="headLogout">
            <a href="javascript:formSubmit()">Log Out</a>
        </td>
    </tr>
</table>